package com.itheima.put;

import com.itheima.domain.Student;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class Test5 {
    public static void main(String[] args) throws IOException {
//        1.把题目三中得到的Student.txt文件中的内容读取到程序中，并封装为Student对象放入ArrayList集合中
//	2.在该过程中遇到的异常全部用try...catch...finally的方式处理
        FileInputStream fis = null;
        ArrayList<Student> list = null;
        String str = null;
        try {
            list = new ArrayList<>();

            fis = new FileInputStream("Student.txt");
            int b;
            byte[] bytes = new byte[1024];
            str = "";
            while ((b = fis.read(bytes)) != -1) {
                str = new String(bytes);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
        String[] split = str.split("}");
        Student s1 = getStudent(split[0]);
        Student s2 = getStudent(split[1]);
        list.add(s1);
        list.add(s2);


    }

    public static Student getStudent(String s) {
        int name = s.indexOf("name='");
        int gender = s.indexOf("', gender='");
        String nameStr = s.substring(name + 6, gender);
        int age = s.indexOf("', age=");
        String genderStr = s.substring(gender + 11, age);
        int score = s.indexOf(", score=");
        String ageStr = s.substring(age + 7, score);
        int ageInt = Integer.parseInt(ageStr);
        String scoreStr = s.substring(score + 8);
        int scoreInt = Integer.parseInt(scoreStr);
        Student s1 = new Student(nameStr, genderStr, ageInt, scoreInt);
        return s1;

    }
}




